package com.zhanghaijun.util;

/**
 * Created by qianchengzhang on 15/8/24.
 */

import com.mangofactory.swagger.configuration.SpringSwaggerConfig;
import com.mangofactory.swagger.models.dto.ApiInfo;
import com.mangofactory.swagger.plugin.EnableSwagger;
import com.mangofactory.swagger.plugin.SwaggerSpringMvcPlugin;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import java.io.File;
import java.util.Properties;

/**
 * 使用注解的方式来扫描API
 * 无需在Spring的xml配置文件来配置，由 @see @EnableWebMvc 代替
 * <p/>
 * <p> @author candle
 * <p/>
 * <p> @date 2015年4月26日 下午1:18:48
 * <p> @version 0.0.1
 */
@Configuration
@PropertySource("classpath:config.properties")
@EnableWebMvc
@EnableSwagger
public class SpringfoxConfig extends WebMvcConfigurerAdapter {

    /**ssß
     * Project Name
     */
    public static String PROJECT_NAME;

    static {
        String projectName = System.getProperty("user.dir");

        System.out.printf("==============================================");
        System.out.println(projectName);

        if (projectName.contains("enniu-manage")) {
            int end = projectName.indexOf(File.separator, projectName.indexOf("enniu-manage"));
            PROJECT_NAME = projectName.substring(projectName.lastIndexOf(File.separator, projectName.indexOf("enniu-manage")) + 1, end == -1 ? projectName.length() : end);
        } else {
            PROJECT_NAME = "app_web";
        }




        System.out.println(PROJECT_NAME);
        try{
            Resource resource = new ClassPathResource("/config.properties");
            Properties settingProp = PropertiesLoaderUtils.loadProperties(resource);
            System.out.println(settingProp);
        }catch (Exception e){
            e.printStackTrace();
        }


    }

    private SpringSwaggerConfig springSwaggerConfig;
    @Value("#{'/monitor/.*,/api/.*'.split(',')}") //适配列表
    private List defaultIncludePatterns ;

    @Value("#{'development'.equalsIgnoreCase('production')}") //判断当前是否是
    private boolean productionMode;

    @Autowired
    public void setSpringSwaggerConfig(SpringSwaggerConfig springSwaggerConfig) {
        this.springSwaggerConfig = springSwaggerConfig;
    }

    /**
     * 链式编程 来定制API样式
     * 后续会加上分组信息
     *
     * @return
     */

    @Bean
    public SwaggerSpringMvcPlugin customImplementation() {
        return new SwaggerSpringMvcPlugin(this.springSwaggerConfig)
                .apiInfo(apiInfo()).enable(!productionMode)
                .includePatterns(
                        (String[]) defaultIncludePatterns.toArray(new String[defaultIncludePatterns.size()]))
                .apiVersion("v1");
        //.swaggerGroup(PROJECT_NAME);
    }

    private ApiInfo apiInfo() {
        ApiInfo apiInfo = new ApiInfo(
                PROJECT_NAME + " API",
                PROJECT_NAME + " Monitor API文档",
                "http://127.0.0.1:8090/swagger-ui/index.html",
                "qianchengzhang@u51.com",
                "U51 License",
                "U51 API License URL"
        );
        return apiInfo;
    }


}